package com.cstor.liang.web.controller;

import com.cstor.liang.config.Auth;
import com.cstor.liang.config.CurrentUser;
import com.cstor.liang.pojo.entity.User;
import com.cstor.liang.pojo.other.*;
import com.cstor.liang.service.UserService;
import com.cstor.liang.utils.WebUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by Administrator on 2017/6/23.
 */
@RestController
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = Logger.getLogger(UserController.class);
	
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResultJo login(@Valid UserLoginVo userLoginVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //获取 Token
        String token = userService.login(
                userLoginVo.getUsername(), userLoginVo.getPassword());
        //未抛出任何错误，登录成功
        return ResultJo.returnOk((Object) token);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo update(@Valid UserModifyVo userModifyVo, BindingResult error,
                           @CurrentUser User user) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //修改自己的用户信息
        userService.updateUserInfo(userModifyVo, user, false);

        //代码未抛出错误，修改成功
        return ResultJo.returnOk("修改成功");
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResultJo add(@Valid UserVo userVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //新增一个用户
        userService.add(userVo);

        //代码未抛出错误，用户注册成功
        return ResultJo.returnOk("新增用户成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth(role = "admin")
    public ResultJo delete(Long id) {
        //数据校验
        if (id == null){
            //校验失败，收集所有错误
            return ResultJo.returnError(1, "缺少id");
        }

        //删除一个用户
        userService.delete(id);

        //代码未抛出错误，删除成功
        return ResultJo.returnOk("删除成功");
    }

    @RequestMapping(value = "/updateOthers", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth(authority = "user_update")
    public ResultJo update(@Valid UserModifyVo userModifyVo, BindingResult error, Long userId) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //修改其他用户信息
        userService.updateUserInfo(userModifyVo, userId);

        //代码未抛出错误，修改成功
        return ResultJo.returnOk("修改成功");
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth(authority = "user_select")
    public ResultJo list(@Valid PageVo pageVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        return ResultJo.returnOk(
                userService.list(pageVo.getPageIndex(), pageVo.getPageSize()));
    }

    @RequestMapping(value = "/listByRole", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth(authority = "user_select")
    public ResultJo listByRole(Integer roleId, @Valid PageVo pageVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }
        if (roleId == null) {
            return ResultJo.returnError(1, "缺少roleId");
        }

        return ResultJo.returnOk(
                userService.listByRole(roleId, pageVo.getPageIndex(), pageVo.getPageSize()));
    }


    @RequestMapping(value = "/listByKeyword", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth(authority = "user_select")
    public ResultJo listByKeyword(String keyword, @Valid PageVo pageVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }
        if (keyword == null) {
            return ResultJo.returnError(1, "缺少keyword");
        }

        return ResultJo.returnOk(
                userService.listByKeyword(keyword, pageVo.getPageIndex(), pageVo.getPageSize()));
    }

    @RequestMapping(value = "/myInfo", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo myInfo(HttpServletRequest request) {
        return ResultJo.returnOk(request.getAttribute("user"));
    }

}
